package com.emar.adx.yiqifa.java;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: xiaoliang
 * @CreateDate: 2015/12/28 11:28
 */
public class RegexDemo {

    private static final Logger logger = LoggerFactory.getLogger(RegexDemo.class);



    public void test1(){
        String s = "<test><?xml version=\"1.0\" encoding=\"utf-8\"?><OrderResult><PageIndex>0</PageIndex><TotalPageCount>1</TotalPageCount><ItemCount>70</ItemCount><OrderList><Order><OrderNumber>MN9042605</OrderNumber><ChannelCode>01006_04001047</ChannelCode><SubChannelCode>NjU3NTcyfDAwY2Y4YjY2NTY0OWJjM2RhNzBh</SubChannelCode><CreateTime>2015-11-01 22:01:01</CreateTime><UnitID>14804</UnitID><CheckInDate>2015-11-04</CheckInDate><CheckOutDate>2015-11-05</CheckOutDate><TotalAmount>538.0000</TotalAmount><Status>1</Status></Order><Order><OrderNumber>MN9042603</OrderNumber><ChannelCode>01006_04001047</ChannelCode><SubChannelCode>NjU3NTcyfDAwYTE2MmE4NDE5ZDEwMDM3N2M5</SubChannelCode><CreateTime>2015-11-01 21:56:12</CreateTime><UnitID>14804</UnitID><CheckInDate>2015-11-04</CheckInDate><CheckOutDate>2015-11-05</CheckOutDate><TotalAmount>538.0000</TotalAmount><Status>1</Status></Order></OrderList></OrderResult></test>";

        String str = "<test>$_$</test>";
        String str1 = "$_$</test>";
        String str2 = "<test>$_$";
        String str3 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>$_$<test>$_$</test>";

        String[] test = str3.split("\\$_\\$");

        Pattern p = Pattern.compile("^$_$\\.*");
        Matcher m = p.matcher(str);
        boolean b = m.find();
        p.matcher(str1);
        boolean b1 = m.find();
        p.matcher(str2);
        boolean b2 = m.find();

        StringBuffer sb = new StringBuffer();
        "".split("test");
        System.out.print(sb);
    }

    public void test2(){
        String reg = "当前页：[1-9]\\d*";
        String word = "当前页：12 teststest,当前页：12 teststest";

        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(word);
        boolean b = m.find();
        logger.debug("is ok:"+b);
        String s1 = m.group();
        logger.debug("str = " + s1);

        System.out.print(s1);
    }

    public void test3(){
        String reg = "NjIwNjQyfDAwN2ZiNTdkMzY5OGFmMmJhNDIw||2016-01-20 19:07:58||10102546812||2213861||B1||2||9.90";
//        Pattern p = Pattern.compile(reg);
//        Matcher m = p.matcher("||");
//        boolean b = m.find();
//
//        logger.debug("is ok:"+b);

//        String s1 = m.group();
        String[] s1 = reg.split("||");
        logger.debug("str = " + s1);
//        System.out.print(s1);
    }


    public void testDateTime(){
        //1.ms毫秒 ,头尾去空格，数字13位
        String regMs = "^\\s*\\d{13}\\s*$";
        String ms = String.valueOf(new Date().getTime());
        Pattern p = Pattern.compile(regMs);
        Matcher m = p.matcher(ms);

        boolean b = m.find();
        logger.debug(ms + " pattern "+ regMs +" : "+b);
        String str = m.group();
        logger.debug("pattern str = "+str);

        //2.m秒
        String regS = "^\\s*\\d{10}\\s*$";
        String s = String.valueOf(new Date().getTime()/1000);
        p = Pattern.compile(regS);
        m = p.matcher(s);

        b = m.find();
        logger.debug(s + " pattern "+ regS +" : "+b);
        str = m.group();
        logger.debug("pattern str = "+str);

        //3.yyyy-MM-dd  简单匹配，没有判断月份
        String reg_yyyy_MM_dd = "^\\s*\\d{4}-\\d{2}-\\d{2}\\s*$";
        String yyyy_MM_dd = "2015-11-11";
        p = Pattern.compile(reg_yyyy_MM_dd);
        m = p.matcher(yyyy_MM_dd);

        b = m.find();
        logger.debug(yyyy_MM_dd + " pattern "+ reg_yyyy_MM_dd +" : "+b);
        str = m.group();
        logger.debug("pattern str = "+str);

        //4.yyyyMMdd

        //5.yyyy-MM-dd HH:mm:ss

        //6.yyyyMMdd HHmmss


    }

    public void test4(){
        String reg = "([\\w0-9]+)\\|\\w+";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher("254|test123");
        if(m.matches()){
            logger.debug(m.groupCount()+"");
            logger.debug(m.group());
        }
        boolean b = m.find();
        logger.debug("is ok:"+b);
        String s1 = m.group();
        logger.debug("str = "+s1);
    }

    public void test5(){
        String email = "killme2008@gmail.com";
        Map<String, Object> env = new HashMap<String, Object>();
        env. put("email", email);
        String username = (String) AviatorEvaluator.execute("email=~/([\\w0-8]+)@\\w+[\\. \\w+]+/ ? $1:'unknow'", env);
        System. out. println(username);
    }

    public  void test6(){
        String ss = "charid=\"42953967949911\" charname=\"\uD83D\uDD31孫語彤\uD83D\uDD31\" return=\"true\"";
        String temp = RegexDemo.getUTF8XMLString(ss);
        System.out.print(temp);
    }

    public static String getUTF8XMLString(String xml) {
        // A StringBuffer Object
        StringBuffer sb = new StringBuffer();
        sb.append(xml);
        String xmString = "";
        String xmlUTF8="";
        try {
            xmString = new String(sb.toString().getBytes("UTF-8"));
            xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");
            System.out.println("utf-8 编码：" + xmlUTF8) ;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // return to String Formed
        return xmlUTF8;
    }

    public void test7(){
        String s1 = "/wanmei/manager/user";
        String s2 = "/wanmei/manager/";
        boolean is = s1.startsWith(s2);
        logger.debug(is+"");
    }

    public static void main(String[] args) {

        RegexDemo demo = new RegexDemo();
//        demo.test1();
//        demo.test2();
//        demo.test3();
//        demo.test4();
//        demo.test5();
//        demo.test6();
        demo.test7();

//        demo.testDateTime();

    }

}
